package cn.ucuser.music.utils;

import cn.ucuser.music.model.PlayList;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.*;

/**
 * @program: music
 * @description:
 * @author: HaoHan
 * @create: 2020-03-19
 **/
public class Music {
    private final String MUSIC_163_COM = "http://music.163.com/weapi/";

    private String conn(Map<String,Object> map,String url){
        try {
            return HttpClient.getConnection(MUSIC_163_COM+url, AES.getPrepare(map));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private String url(String id) {
        String url = "song/enhance/player/url?csrf_token=";
        String[] urls = { id };
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("ids", urls);
        map.put("br", 999000);
        map.put("csrf_token", "");
        return conn(map,url);
    }

    private String detail(String id) {
        String url = "v3/song/detail?csrf_token=";
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, String> ids = new HashMap<String, String>();
        ids.put("id", id);
        map.put("c", "[" + AES.jsonEncode(ids) + "]");
        map.put("csrf_token", "");
        return conn(map,url);
    }

    private String lyric(String id) {
        String url = "song/lyric";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("os", "pc");
        map.put("lv", -1);
        map.put("kv", -1);
        map.put("tv", -1);
        return conn(map,url);
    }

    private String playlist(String id) {
        String url = "playlist/detail";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        return conn(map,url);
    }
    //失效转移到getJSON
    private PlayList getPlayInfo(String idStr){
        String detail = detail(idStr);
        JSONObject object = JSON.parseObject(detail);

        Object songs = object.getJSONArray("songs").get(0);
        JSONObject json = JSON.parseObject(songs.toString());

        Object musicId = json.get("id");
        Object musicName = json.get("name");

        String ar = json.getJSONArray("ar").get(0).toString();
        JSONObject o = JSON.parseObject(ar);
        Object artistName = o.get("name");

        JSONObject al = json.getJSONObject("al");
        Object pic = al.get("pic");
        Object albumName = al.get("name");

        PlayList p = new PlayList();
//        p.setPid(pic.toString());
        p.setLrc("/music/getLrc?id="+musicId.toString());
        p.setTitle(musicName.toString());
        p.setAlbum(albumName.toString());
        p.setMid(musicId.toString());
        p.setArtist(artistName.toString());

        return p;
    }

    public String getMp3(String id){
        JSONObject json = JSON.parseObject(url(id));

        Object data = json.getJSONArray("data").get(0);
        JSONObject jsonObject = JSON.parseObject(data.toString());

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("url",jsonObject.get("url"));
        map.put("br",jsonObject.get("br"));

        return JSON.toJSONString(map).replace("http:","");
    }//music/getMp3?id=" + H.mid//"music/getPicImg?id=" + H.mid

    public String getPicImg(String id){
        JSONObject object = JSON.parseObject(detail(id));

        Object songs = object.getJSONArray("songs").get(0);
        JSONObject json = JSON.parseObject(songs.toString());
        JSONObject al = json.getJSONObject("al");

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("url",al.get("picUrl"));

        return JSON.toJSONString(map).replace("http:","");//pic
    }

    public String getLyric(String id){
        JSONObject lrc = JSON.parseObject(lyric(id)).getJSONObject("lrc");
        return lrc.get("lyric").toString();
    }

    public String getJSON(String songList){
        List<PlayList> lists = new ArrayList<PlayList>();

        JSONArray tracks = JSON.parseObject(playlist(songList))
                .getJSONObject("result")
                .getJSONArray("tracks");

        for (int i = 0; i < tracks.size(); i++) {
            JSONObject jsonObject1 = tracks.getJSONObject(i);

            Object id = jsonObject1.get("id");//歌曲ID
            JSONObject album = jsonObject1.getJSONObject("album");
            Object artist = album.getJSONArray("artists").getJSONObject(0).get("name");//歌手

            PlayList p = new PlayList();
            p.setLrc("/music/getLrc?id="+id);//歌词
            p.setTitle(jsonObject1.get("name"));//歌曲名
            p.setAlbum(album.get("name"));//专辑
            p.setMid(id);
            p.setArtist(artist);
            lists.add(p);
        }
        return JSON.toJSONString(lists);
    }
}
